import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import check.*;
import check.Teacher_InsertScore;
import view.*;

import javax.swing.*;


public class UserView {
    //用户id
    private String id;
    //数据库
    private Connection connection;
    private JFrame userView = new JFrame("界面");
    private JPanel panel = new JPanel(new FlowLayout());
    public int number;
    //添加功能选择
    private JButton studentInformation = new JButton("学生信息");
    private JButton Student_Course = new JButton("学生选课");
    private JButton Teacher_Information = new JButton("老师信息");
    private JButton Teacher_CheckScore = new JButton("查看分数");
    private JButton Teacher_InsertScore = new JButton("登记分数");
    private JButton function6 = new JButton("数据库备份");
    //先声明功能的选择，到以后写好包之后即可删除
    private JFrame fuction5_View;
    private JFrame fuction6_View;

    public UserView(int number, Connection connection, String id) {
        this.connection = connection;
        this.number = number;
        this.id = id;
        init();
    }

    public void init() {

        if (this.number == 1) {
            studentInformation.setFont(new Font("", Font.BOLD, 15));
            panel.add(studentInformation);
            Student_Course.setFont(new Font("", Font.BOLD, 15));
            panel.add(Student_Course);
        } else {
            Teacher_Information.setFont(new Font("", Font.BOLD, 15));
            panel.add(Teacher_Information);
            Teacher_InsertScore.setFont(new Font("", Font.BOLD, 15));
            panel.add(Teacher_InsertScore);
            Teacher_CheckScore.setFont(new Font("", Font.BOLD, 15));
            panel.add(Teacher_CheckScore);
            function6.setFont(new Font("", Font.BOLD, 15));
            panel.add(function6);
        }


        //添加佛大图片
        JLabel Fosu = new JLabel(new ImageIcon("src/img.png"));
        panel.add(Fosu);
        Fosu.setBounds(0, 150, 700, 200);
        //添加文字
        JLabel showName = new JLabel("成绩管理系统");
        showName.setFont(new Font("", Font.BOLD, 22));
        panel.add(showName);
        userView.add(panel);
        userView.setSize(500, 500);
        userView.setLocationRelativeTo(null);
        userView.setVisible(true);
        userView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /*
         * 给按钮添加事件监听器
         */
        //学生信息页面
        studentInformation.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userView.setVisible(false);
                Student_Information student_information = new Student_Information(connection, id, userView);
                student_information.studentView.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        userView.setVisible(true);
                        userView.setLocationRelativeTo(null);
                        super.windowClosing(e);
                    }
                });

            }
        });
        //学生选课页面
        Student_Course.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //这里为功能输入，后期直接写好包之后导入即可
                userView.setVisible(false);
                Student_Course course1 = new Student_Course(connection, id);
                course1.course.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        userView.setVisible(true);
                        userView.setLocationRelativeTo(null);
                        super.windowClosing(e);
                    }
                });

            }
        });
        //老师信息页面
        Teacher_Information.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userView.setVisible(false);
                Teacher_Information course1 = new Teacher_Information(connection, id, userView);
                course1.teacherView.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        userView.setVisible(true);
                        userView.setLocationRelativeTo(null);
                        super.windowClosing(e);
                    }
                });
            }
        });
        //登记分数。
        Teacher_InsertScore.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userView.setVisible(false);
                Teacher_InsertScore teacher_insertScore=new Teacher_InsertScore(id);
                teacher_insertScore.jFrame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        userView.setVisible(true);
                        userView.setLocationRelativeTo(null);
                        super.windowClosing(e);
                    }
                });

            }
        });
        //检查分数
        Teacher_CheckScore.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userView.setVisible(false);
                Teacher_CheckScore teacher_checkScore = new Teacher_CheckScore(connection, id);
                teacher_checkScore.jf.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        userView.setVisible(true);
                        userView.setLocationRelativeTo(null);
                        super.windowClosing(e);
                    }
                });
            }
        });
        //功能六页面
        function6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //这里为功能输入，后期直接写好包之后导入即可
//                fuction2_View = Singleton2.getInstance(); 单例模式
                fuction6_View = new JFrame();
                fuction6_View.setTitle("第二个窗口");
                fuction6_View.setSize(500, 500);
                fuction6_View.setLocationRelativeTo(null);
                userView.setVisible(false);
                fuction6_View.setVisible(true);
                fuction6_View.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        userView.setVisible(true);
                        super.windowClosing(e);
                    }
                });
            }
        });

    }


}